package ui.popup 
{
	import events.GCEvent;
	import ui.button.GCButton;
	import ui.GCNumberScroller;
	import ui.textfield.GCTextField;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Roy
	 */
	public class GCInventoryPopup extends Sprite 
	{
		public static const ACTION_LOADING_SHIP:uint = 0;
		public static const ACTION_UNLOADING_SHIP:uint = 1;
		public static const ACTION_DISCARDING:uint = 2;
		
		private static const ACTION_LOADING_LABEL:String = "Move to Ship";
		private static const ACTION_UNLOADING_LABEL:String = "Move to Warehouse";
		private static const ACTION_DISCARD_LABEL:String = "Discarding Item";
		private static const RESOURCE_LABEL:String = "Name of Good:";
		private static const QUANTITY_LABEL:String = "Set Quantity:";
		private static const MAX_QUANTITY_LABEL:String = "Max Quantity:";
		
		private static const LABEL_HEIGHT:int = 50;
		private static const LABEL_WIDTH:int = 100;
		
		/*
		Clickable components in the menu. 
		*/
		private var _quantityComponent:GCNumberScroller;
		private var _confirmButton:GCButton;
		private var _cancelButton:GCButton;
		private var _setMaxButton:GCButton;
		
		/*
		Variables used to keep track of item information. Should be reset
		everytime this popup reappears
		*/
		private var _resourceName:String;
		private var _quantity:int;
		private var _actionType:uint;
		private var _maxQuantity:uint;
		
		/*
		Various text labels
		*/
		private var _actionLabel:GCTextField;
		private var _resourceLabel:GCTextField;
		private var _quantityLabel:GCTextField;
		private var _maxQuantityLabel:GCTextField;
		
		/*
		Labels to display values
		*/
		private var _resourceText:GCTextField;
		
		public function GCInventoryPopup(x:int = 0, y:int = 0) 
		{
			this.x = x;
			this.y = y;
			
			_quantityComponent = new GCNumberScroller();
			_confirmButton = new GCButton(onConfirm);
			_cancelButton = new GCButton(onCancel);
			_setMaxButton = new GCButton(onSetToMax);
			_actionLabel = new GCTextField();
			_resourceLabel = new GCTextField();
			_quantityLabel = new GCTextField();
			_maxQuantityLabel = new GCTextField();
			
			_resourceName = "";
			_quantity = 0;
			
			_resourceText = new GCTextField();
			
			init();
		}
		
		
		private function init():void
		{
			// Listeners
			_quantityComponent.addEventListener(GCEvent.VALUE_CHANGED_EVENT, onValueChanged);			
			
			// Labels
			
			_resourceLabel.height = GCConstants.LABEL_HEIGHT;
			_resourceLabel.width = GCConstants.LABEL_WIDTH;
			_resourceLabel.x =  GCConstants.X_OFFSET;
			_resourceLabel.y =  GCConstants.Y_OFFSET;
			_resourceLabel.setStyle(GCTextField.TEXT_TYPE_LEFT);
			
			_maxQuantityLabel.width = LABEL_WIDTH;
			_maxQuantityLabel.height = LABEL_HEIGHT;
			_maxQuantityLabel.x = _resourceLabel.x;
			_maxQuantityLabel.y = _resourceLabel.y + _resourceLabel.height;
			_maxQuantityLabel.setStyle(GCTextField.TEXT_TYPE_LEFT);
			
			_quantityLabel.height = GCConstants.LABEL_HEIGHT;
			_quantityLabel.width = GCConstants.LABEL_WIDTH;
			_quantityLabel.x =  GCConstants.X_OFFSET;
			_quantityLabel.y =  _maxQuantityLabel.y + _maxQuantityLabel.height;
			_quantityLabel.setStyle(GCTextField.TEXT_TYPE_LEFT);
			
			_resourceText.width = LABEL_WIDTH;
			_resourceText.height = LABEL_HEIGHT;
			_resourceText.x = _resourceLabel.x + _resourceLabel.width;
			_resourceText.y = _resourceLabel.y;
			_resourceText.setStyle(GCTextField.TEXT_TYPE_LEFT);
			
			// Number Scroll Component
			_quantityComponent.x = _quantityLabel.x + _quantityLabel.width;
			_quantityComponent.y = _quantityLabel.y;
			
			// Buttons
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("OK");
			_confirmButton.y = _quantityLabel.y + GCConstants.LABEL_HEIGHT;
			
			_cancelButton.loadGraphic(GCResources.plate, 100, 30);
			_cancelButton.loadText("Cancel");
			_cancelButton.y = _confirmButton.y;
			_cancelButton.x = _confirmButton.x + _confirmButton.width;
			
			_setMaxButton.loadGraphic(GCResources.plate, 100, 30);
			_setMaxButton.loadText("Set Max");
			_setMaxButton.x = _maxQuantityLabel.x + _maxQuantityLabel.width;
			_setMaxButton.y = _maxQuantityLabel.y;
			
			this.addChild(_actionLabel);
			this.addChild(_confirmButton);
			this.addChild(_cancelButton);
			this.addChild(_quantityComponent);
			this.addChild(_resourceLabel);
			this.addChild(_quantityLabel);
			this.addChild(_resourceText);
			this.addChild(_maxQuantityLabel);
			this.addChild(_setMaxButton);
			
			// Popup background
			var back:Sprite = GCResources.getMaskedCanvasBackground(this.width, this.height);
			this.addChildAt(back, 0);
			
			_actionLabel.height = 30;
			_actionLabel.width = this.width;
			
			_resourceLabel.text = RESOURCE_LABEL;
			_quantityLabel.text = QUANTITY_LABEL;
			_resourceText.text = _resourceName;
		}
		
		/** Setting up the popup for an item **/
		public function initializePopup(maxQuantity:int, resourceName:String, actionType:uint):void
		{
			_maxQuantity = maxQuantity;
			_quantityComponent.setLimit(maxQuantity);
			_resourceName = resourceName;
			_actionType = actionType;
			setPopupActionLabel();
			
			_maxQuantityLabel.text = MAX_QUANTITY_LABEL + " " + String(_maxQuantity);
			_resourceText.text = _resourceName;
			
			// Enable the buttons
			_cancelButton.enable();
			_confirmButton.enable();
			_setMaxButton.enable();
		}
		
		public function clearPopup():void
		{
			_resourceName = "";
			_quantityComponent.setValue(0);
		}
		
		/** Get Functions **/
		public function getQuantity():int
		{
			return _quantity;
		}		
		
		/** Listener Functions **/
		private function onConfirm(event:MouseEvent):void
		{
			// Disable the buttons
			_cancelButton.disable();
			_confirmButton.disable();
			_setMaxButton.disable();
			
			if (_quantity > 0)
			{
				switch (_actionType)
				{
					case ACTION_LOADING_SHIP:
						dispatchEvent(new Event(GCEvent.LOAD_CONFIRM_EVENT));
						break;
					case ACTION_UNLOADING_SHIP:
						dispatchEvent(new Event(GCEvent.UNLOAD_CONFIRM_EVENT));
						break;
					case ACTION_DISCARDING:
						dispatchEvent(new Event(GCEvent.DISCARD_CONFIRM_EVENT));
						break;
					default:
				}
			}
			else
			{
				dispatchEvent(new Event(GCEvent.CANCEL_EVENT));
			}
		}
		
		private function onCancel(event:MouseEvent):void
		{
			// Disable the buttons
			_cancelButton.disable();
			_confirmButton.disable();
			_setMaxButton.disable();
			
			dispatchEvent(new Event(GCEvent.CANCEL_EVENT));
		}
		
		private function onValueChanged(event:Event):void
		{
			_quantity = _quantityComponent.value;			
		}
		
		private function onSetToMax(event:MouseEvent):void
		{
			_quantityComponent.setValue(_maxQuantity);
		}
		
		private function setPopupActionLabel():void
		{
			switch (_actionType)
			{
				case ACTION_LOADING_SHIP:
					_actionLabel.text = ACTION_LOADING_LABEL;
					break;
				case ACTION_UNLOADING_SHIP:
					_actionLabel.text = ACTION_UNLOADING_LABEL;
					break;
				case ACTION_DISCARDING:
					_actionLabel.text = ACTION_DISCARD_LABEL;
					break;
				default:
			}
		}
	}

}